/** Adding "disable" on expand button for cloudAPI **/
$.fn.extend( {

	checkCheckbox:function( checked )
	{
		// In case of checkbox we have the div and the input
		var element = $(this);
		
		// In case of div getting the checkbox
        if ( $(this).is("div"))
        {
			// NOTE this will work if there is only one checkbox under this parent.
			// If you want to handle the case of couple of checkboxes under same parent, we need to 
			// find the checkbox by it's id "ocb_..."        
        	element = $(this).parent().children('[type=checkbox]');	
        }
         	
		if (!checked)
		{
			element.removeAttr("checked");
			element.parent().children(".checkboxAreaChecked").removeClass("checkboxAreaChecked").addClass("checkboxArea").removeAttr("checked");
		}
		else
		{
			element.attr("checked","checked");
			element.parent().children(".checkboxArea").removeClass("checkboxArea").addClass("checkboxAreaChecked").attr("checked","checked");
		}
	},
	
	disableCheckbox:function(){
		if ($(this).isCheckboxEnabled())
		{
			$( this ).fadeTo(1, 0.3);
			var mouseup = $(this).attr("onmouseup");
			$(this).attr("oldmouseup",mouseup);
			$(this).removeAttr("onmouseup");
			$(this).attr("disabled","disabled");
		}
	},
	
	enableCheckbox:function(){
		if (!$(this).isCheckboxEnabled())
		{
			$( this ).fadeTo(0.3,1);
			var mouseup = $(this).attr("oldmouseup");
			$(this).attr("onmouseup",mouseup);
			$(this).removeAttr("oldmouseup");
			$(this).removeAttr("disabled");
		}
	},
	
	isCheckboxEnabled:function(){
		return $(this).attr("oldmouseup") === undefined
	},
	
	
    disableButton:function()
    {
        $(this).addClass("disabled-button");
        if ( $(this).is("div"))
        {
            $( this ).fadeTo(1, 0.3);
            if ( $(this).attr("origOnClick") === undefined ) // make this code rerunable
            {
                $(this).attr("origOnClick",$(this).attr("onclick"));
            }
            $(this).removeAttr("onclick");

        }
        if ( $( this ).is( "a" ) )
        {
            $( this ).fadeTo( 1, 0.3 );
            $( this ).click( function( event )
            {
                event.preventDefault()
            } );
            if ( $(this).attr("origOnClick") === undefined ) // make this code rerunable
            {
                $(this).attr("origOnClick",$(this).attr("onclick"));
            }
            $(this).removeAttr("onclick");
        }
        $( this ).attr( 'disabled', 'disabled' );

    },

    enableButton:function()
    {
        $(this).removeClass("disabled-button");
        if ( $( this ).is("div"))
        {
            $( this ).fadeTo(0.3,1);
            if ( $(this).attr("origOnClick") !== undefined )
            {
                $(this).attr("onclick",$(this).attr("origOnClick"));
                $(this).removeAttr("origOnClick");
            }
        }
        if ( $( this ).is( "a" ) )
        {
            $( this ).fadeTo( 1, 1 );
            $( this ).removeAttr( 'onclick' ).unbind( 'click' );
             if ( $(this).attr("origOnClick") !== undefined )
            {
                $(this).attr("onclick",$(this).attr("origOnClick"));
                $(this).removeAttr("origOnClick");
            }
        }
        $( this ).removeAttr( 'disabled' );
    },

    isButtonDisabled:function()
    {
        return $( this ).attr( 'disabled' ) == 'disabled'
    },

    isButtonEnabled:function()
    {
        return $(this) && $(this).length > 0 && $( this ).attr( 'disabled' ) !=  'disabled'
    },

    disableSelect:function()
    {
        var selectArea = $( this ).parent().children(".selectArea");
        selectArea.fadeTo(1,0.4);
        selectArea.children(".selectButton").removeAttr("href");
    },


    isTinyScrollDisabled:function()
    {
        // guy - we need to know this. please take good care of not giving "disable" as classname to a child
        return $(this).children(".scrollbar").is(".disable")
    },


    tinyscrollbar_updateIfNotDisabled: function( offsetIfDisabled, offerIfNotDisabled )
    {
        if ( $(this).isTinyScrollDisabled() )
        {
            $(this).tinyscrollbar_update(offsetIfDisabled)
        }
        else
        {
            $(this).tinyscrollbar_update(offerIfNotDisabled);
        }
    },

    markInputFieldAsError: function( hasError, data )
    {
        var className = "pink";
        var elementToPink = $( this ).hasClass("exceptionDialogInputText") ? $( this ) : $(this).parent();
        if ( $( this ).parent().hasClass("long-input") || $(this).hasClass("exceptionDialogInputText") )
        {
            className = "long-pink";
        }



        $( this ).attr("tiptip", "tiptip" );   // add attribute for CSS

        if ( hasError )
        {

            elementToPink.addClass( className );
        }
        else
        {
            elementToPink.removeClass( className )
        }

        if ( $( this ).attr("tiptip") !== undefined  )// make sure CSS applies to this object
        {
            var toExtend = data && data.hasOwnProperty("tipTip") ? data["tipTip"] : {};
            $( this ).tipTip($.extend({ edgeOffset: (hasError ? 3 : -9999), defaultPosition:'left', activation:'focus'},toExtend));
            if ( hasError )
            {
                $( this ).trigger('focus');
            }
            else
            {
                $("#tiptip_holder").hide();
            }
        }

    },

    /** use this with 'keypress' on input fields, to disable associated buttons **/
    disableAssociatedButtonOnKeyPress: function( button )
    {
        $( this ).keypress( function( event )
        {

            if ( $.isKeyPressEnter(event) && !button.isButtonDisabled() )
            {
                if ( button.attr( "href" ) && typeof(button.attr( "href" )) != "undefined" && button.attr( "href" ).indexOf( "javascript" ) >= 0 )
                {
                    if ( button.trigger( 'click' ) ) // support pre validation for links
                    {
                        window.location = button.attr( "href" );
                    }
                }
                else
                {
                    button.trigger( 'click' );
                }
            }
            else
            {
                button.disableButton();
            }
        } );

        return $( this );
    }
} );

$.extend({
       isKeyPressEnter : function(event)
    {
   return event && typeof(event) != "undefined" && event.hasOwnProperty("keyCode") && event.keyCode == '13';

    },
    isSuggestSelected : function()
    {
        return $(".jsonSuggest .ui-state-hover").length > 0;
    },
    clearSuggestSelection: function()
    {
        $(".jsonSuggest .ui-state-hover").removeClass("ui-state-hover");
    }
});

/** http://jarrodoverson.com/js/plugins/jquery.pulse.js **/
jQuery.fn.pulse = function( properties, duration, numTimes, interval )
{

    if ( duration === undefined || duration < 0 ) duration = 500;
    if ( duration < 0 ) duration = 500;

    if ( numTimes === undefined ) numTimes = 1;
    if ( numTimes < 0 ) numTimes = 0;

    if ( interval === undefined || interval < 0 ) interval = 0;

    return this.each( function()
    {
        var $this = jQuery( this );
        var origProperties = {};
        for ( property in properties )
        {
            try
            {
                origProperties[property] = $this.css( property );
            }
            catch( error )
            {
                console.log( "error while getting orig properties : " + error )
            }
        }

        var subsequentTimeout = 0;
        for ( var i = 0; i < numTimes; i++ )
        {
            window.setTimeout( function()
            {
                try
                {
                    $this.animate(
                            properties,
                    {
                        duration:duration / 2,
                        complete:function()
                        {
                            try
                            {
                                $this.animate( origProperties, duration / 2 )
                            }
                            catch( error )
                            {
                                console.log("error while animating on complete : " + error)
                            }
                        }

                    }
                            );
                }
                catch( error )
                {
                    console.log( "error while animating :" + error )
                }
            }, (duration + interval) * i );
        }
    } );

};



// http://groups.google.com/group/jquery-ui/browse_thread/thread/35a33d1c50f0e724/e527c8f47ba183e0 - at the bottom
jQuery.fn.centerObjectOnScreen = function( loaded )
{
    var obj = this;
    if ( !loaded )
    {

        obj.css( 'top', $(window).scrollTop() + ( $( window ).height() / 2 - this.height() / 2 ) );
        obj.css( 'left', $( window ).width() / 2 - this.width() / 2 );
        $( window ).resize( function()
        {
            obj.centerObjectOnScreen( !loaded );
        } );
        $(window).scroll(function(){obj.centerObjectOnScreen(!loaded)});
    }
    else
    {
        obj.stop();
        obj.animate( { top: $(window).scrollTop() + ($( window ).height() / 2 - this.height() / 2), left: $( window ).width() / 2 - this.width() / 2}, 200, 'linear' );
    }
};


// "select text" function
jQuery.fn.selText = function() {
	var obj = this[0];
	if ($.browser.msie) {
		var range = obj.offsetParent.createTextRange();
		range.moveToElementText(obj);
		range.select();
	} else if ($.browser.mozilla || $.browser.opera) {
		var selection = obj.ownerDocument.defaultView.getSelection();
		var range = obj.ownerDocument.createRange();
		range.selectNodeContents(obj);
		selection.removeAllRanges();
		selection.addRange(range);
	} else if ($.browser.safari) {
		var selection = obj.ownerDocument.defaultView.getSelection();
		selection.setBaseAndExtent(obj, 0, obj, 1);
	}
	return this;
};

jQuery.fn.editInPlace = function( isEdit ) {
    var obj = this;
    var viewMode = "editable-item-view";
    var editMode = "editable-item-edit";
    if ( !obj.hasClass(viewMode) && !obj.hasClass(editMode))
    {
        if ( obj.closest("."+viewMode).length > 0)
        {
            obj = obj.closest("."+viewMode);
        }
        else if ( obj.closest("."+editMode).length > 0)
        {
            obj = obj.closest("."+editMode);
        }
        else
        {
            console.log("only able to operate on " + viewMode + " or " + editMode);
            return;
        }
    }

    if ( isEdit )
    {
        obj.removeClass(viewMode);
        obj.addClass(editMode);
    }

    else
    {
        obj.removeClass(editMode);
        obj.addClass(viewMode)
    }
    return $(this);
};
